<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=sretail";
echo $act;
// -- validasi
if(($act =='add') || ($act == 'edit')) { 
	if (trim($_POST['noreferensi']) == '') {
		$error[] = '- No Referensi harus diisi !!!';
	}
	if (trim($_POST['tanggal']) == '') {
		$error[] = '- Tanggal harus diisi !!!';
	}
	if (trim($_POST['dari']) == '') {
		$error[] = '- Customer harus dipilih !!!';
	}
	if (trim($_POST['barang']) == '') {
		$error[] = '- Paket Sewa harus dipilih !!!';
	}
	if (trim($_POST['kendaraan']) == '') {
		$error[] = '- Kendaraan harus dipilih !!!';
	}
	if (trim($_POST['transaksi']) == '') {
		$error[] = '- Detail Transaksi harus diisi !!!';
	}
	if ($_POST['ntotal'] <= 0) {
		$error[] = '- Periksa Total Transaksi anda !!!';
	}
}
else if ($act == 'bayar')
{
if (trim($_POST['keterangan']) == '') {
		$error[] = '- Keterangan harus diisi !!!';
	}

}
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	switch($act) {
	case("delete"):
		$query = "delete from gl_rtrans where no_ref='$_POST[noreferensi]'";
		$query2 = "delete from gl_trans where no_ref='$_POST[noreferensi]'";
		// hapus data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			// ---
			mysql_query($query2, $con_gl);
			// ---
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil dihapus ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal dihapus !!!";
		}
	break;
	case("bayar"):
		$today = date("Y-m-d"); 
		$query = "INSERT INTO gl_pembayaran (no_ref, no_transaksi, tgl_bayar, jns_pemb, tipe_pemb, pmb_nominal, hold_status) VALUES 				                 ('$_POST[no_ref]','notrx','$today', '$_POST[cr_jenisbayar]','$_POST[cr_tipebayar]','$_POST[pembayaran]','$_SESSION[nama]' )";
		
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";

			echo "<script type=\"text/javascript\">window.opener.location.href='".$url."'; window.close();</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	case("edit"):
		mysql_select_db($database_con_gl, $con_gl);
		
		if(strstr($_SESSION['akses'],'A-1-4')) {
			$query_gudang = "SELECT id, gudang, a_piu, a_kas, a_beb, a_paj FROM gl_gudang where id='$_POST[gudang]' ORDER BY gudang ASC"; 
			list($idgudang, $namagudang, $a_piu, $a_kas, $a_beb, $a_paj) = mysql_fetch_row(mysql_query($query_gudang));
			$_POST['akunkredit'] 	= $a_piu;
			$_POST['akunbiaya']		= $a_beb;
			$_POST['akunpajak']		= $a_paj;
		}
		
		$query ="UPDATE gl_rtrans SET tgl='$_POST[tanggal]', keterangan='$_POST[keterangan]', jenis='$_POST[jenis]', area='$_POST[area]', gudang='$_POST[gudang]', barang='$_POST[barang]', kontak='$_POST[dari]', recipient='$_POST[recipient]', jatuhtempo='$_POST[checkout]', tujuan='$_POST[tujuan]', pengemudi='$_POST[pengemudi]', akbiaya='$_POST[akunbiaya]', akpjk='$_POST[akunpajak]', jmlhr='$_POST[jumlah]', jmlwkt='$_POST[jmlhwkt]', checkin='$_POST[checkin]', waktucheckin='$_POST[waktucheckin]', checkout='$_POST[checkout]', waktucheckout='$_POST[waktucheckout]', supir='$_POST[supir]', bbm='$_POST[bbm]', tol='$_POST[tol]', parkir='$_POST[parkir]', akomod='$_POST[akomodasi]', other='$_POST[other]', kendaraan='$_POST[kendaraan]' WHERE no_ref='$_POST[noreferensi]'";
		//---
		$query_getkpd = "SELECT gl_kontak.nama FROM gl_kontak WHERE gl_kontak.id='$_POST[dari]'";
		$getkpd = mysql_query($query_getkpd, $con_gl) or die(mysql_error());
		$row_getkpd = mysql_fetch_assoc($getkpd);

		// --- edit transaksi
		mysql_select_db($database_con_gl, $con_gl);
		$editquery = "UPDATE gl_trans SET transaksi= '$_POST[transaksi]', total= '$_POST[hargasatuan]', barang= '$_POST[barang]', kendaraan='$_POST[kendaraan]', jumlah= '$_POST[jumlah]', hargasatuan='$_POST[xharga]', diskon='$_POST[diskon]' WHERE id='$_POST[id_transaksi]'";
		mysql_query($editquery, $con_gl);
		//-- save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl);
		if($runquery) { 
			$update_tr = mysql_query("update gl_trans set transaksi='$_POST[transaksi]', total='$_POST[hargasatuan]', hargasatuan='$_POST[xharga]', diskon='$_POST[diskon]', kendaraan='$_POST[kendaraan]' where id='$_POST[id_transaksi]'", $con_gl) or die(mysql_error());
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	default:
		mysql_select_db($database_con_gl, $con_gl);		
		

		$query_getkpd = "SELECT gl_kontak.nama FROM gl_kontak WHERE gl_kontak.id='$_POST[dari]'";
		$getkpd = mysql_query($query_getkpd, $con_gl) or die(mysql_error());
		$row_getkpd = mysql_fetch_assoc($getkpd);
		// ---
		$query = "INSERT INTO gl_retailtrans (no_ref, jnsorder,tgl, keterangan, area, gudang, barang, kontak, jenis, jatuhtempo, tujuan, recipient, pengemudi,  jmlhr, jmlwkt, checkin, waktucheckin, checkout, waktucheckout, supir, bbm, tol, parkir, akomod, other, kendaraan) VALUES ('$_POST[noreferensi]','0', '$_POST[tanggal]', '$_POST[keterangan]', '$_POST[area]', '$_POST[gudang]', '$_POST[barang]', '$_POST[dari]', '$_POST[jenis]', '$_POST[checkout]','$_POST[tujuan]', '$_POST[recipient]', '$_POST[pengemudi]',  '$_POST[jumlah]','$_POST[jmlhwkt]','$_POST[checkin]', '$_POST[waktucheckin]', '$_POST[checkout]', '$_POST[waktucheckout]', '$_POST[supir]', '$_POST[bbm]', '$_POST[tol]', '$_POST[parkir]', '$_POST[akomodasi]', '$_POST[other]','$_POST[kendaraan]')";
		// insert piutang
		
		// tambah variabe pajak
		// tambah variable grandtotal	
  			
		mysql_select_db($database_con_gl, $con_gl);
		$addquery = "INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn, hargasatuan, diskon,diskonamount) VALUES (NULL, '$_POST[noreferensi]', '$_POST[transaksi]', '$_POST[hargasatuan]', '$_POST[barang]', '$_POST[kendaraan]', '$_POST[hargaafterppn]','$_POST[xharga]', '$_POST[diskon]','$_POST[txtdiskon]')";
		mysql_query($addquery, $con_gl) or die (mysql_error());
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	}
}
?>